<template>
	<div class="app-container">
		<div class="head-container">
			<div v-if="crud.props.searchToggle">
				<el-input v-model="query.title" clearable size="small" placeholder="按照服务标题搜索" style="width: 200px;"
					class="filter-item" @keyup.enter.native="crud.toQuery" />
				<rrOperation />
			</div>
			<crudOperation :permission="permission" />
		</div>

		<!-- 表单 -->
		<!-- <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="680px"
    >
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="公司名称">
          <el-input v-model="form.companyName" />
        </el-form-item>
        <el-form-item label="联系热线">
          <el-input v-model="form.phoneNumber" />
        </el-form-item>
        <el-form-item label="公司所在城市">
          <el-select v-model="form.companyCity" placeholder="请选择公司所在的城市">
            <el-option
              v-for="(item,index) in cityList"
              :key="index"
              :label="item.value"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="公司地址">
          <el-input v-model="form.address" />
        </el-form-item>
        <el-form-item label="工作时间">
          <el-input v-model="form.workingHours" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog> -->

		<!-- 进度修改 -->
		<el-dialog title="修改进度" :visible.sync="updateScheduleDialog">
			<el-form :model="form">
				<el-form-item label="进度" label-width="120px">
					<el-select v-model="schedule" placeholder="请选择进度" @change="scheduleChange">
						<el-option v-for="(item,index) in scheduleList" :key="index" :label="item.value"
							:value="item.label" />
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="updateScheduleDialog = false">取 消</el-button>
				<el-button type="primary" @click="updateSchedule">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 备注添加 -->
		<el-dialog title="添加备注" :visible.sync="remarkDialog">
			<el-form :model="form">
				<el-form-item label="备注" label-width="120px">
					<el-input v-model="remark" type="textarea" :rows="4" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="remarkDialog = false">取 消</el-button>
				<el-button type="primary" @click="updateRemark">确 定</el-button>
			</div>
		</el-dialog>

		<!--表格渲染-->
		<el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;"
			@selection-change="crud.selectionChangeHandler">
			<el-table-column type="selection" width="55" />
			<el-table-column prop="userName" align="center" label="客户姓名" width="100px" />
			<el-table-column prop="contactNumber" align="center" label="客户联系方式" width="100px" />
			<el-table-column prop="serviceTitle" align="center" label="所属服务" width="200px" />
			<el-table-column prop="type" align="center" label="订单类型" width="100px" />
			<el-table-column prop="orderTime" align="center" label="订单时间" width="200px" />
			<el-table-column prop="schedule" align="center" label="进度" width="100px" />
			<el-table-column prop="updateTime" align="center" label="进度完成时间" width="200px" />
			<el-table-column prop="content" align="center" label="咨询内容" width="200px" />
			<el-table-column prop="remark" align="center" label="备注" width="300px" />
			<el-table-column v-if="checkPer(['admin','menu:del'])" align="center" label="操作" width="300px"
				fixed="right">
				<template slot-scope="scope">
					<udOperation style="display:inline-block;" :data="scope.row" :permission="permission"
						msg="确定删除吗，此操作不能撤销！" />
					<el-popover placement="top-start" title="修改进度" trigger="hover" content="修改订单进度">
						<el-button slot="reference" type="primary" style="display:inline-block;" icon="el-icon-d-caret"
							@click="openUpdateScheduleDialog(scope.row.id,scope.row.schedule)" />
					</el-popover>
					<el-popover placement="top-start" title="添加/修改备注" trigger="hover" content="添加/修改订单备注">
						<el-button slot="reference" type="success" style="display:inline-block;" icon="el-icon-document"
							@click="openRemarkDialog(scope.row.id,scope.row.remark)" />
					</el-popover>
				</template>
			</el-table-column>
		</el-table>
		<!--分页组件-->
		<pagination />
	</div>
</template>

<script>
	import crudOrder from '@/api/finace_taxation/order.js'
	import CRUD, {
		presenter,
		header,
		form,
		crud
	} from '@crud/crud'
	import rrOperation from '@crud/RR.operation'
	import crudOperation from '@crud/CRUD.operation'
	import pagination from '@crud/Pagination'
	import udOperation from '@crud/UD.operation'

	// crud交由presenter持有
	const defaultForm = {
		companyName: null,
		companyCity: null,
		workingHours: null,
		address: null,
		phoneNumber: null,
		permission: null
	}

	export default {
		name: 'Order',
		components: {
			pagination,
			crudOperation,
			rrOperation,
			udOperation
		},
		cruds() {
			return CRUD({
				url: 'api/ft/ftOrder',
				title: '订单管理',
				crudMethod: {
					...crudOrder
				}
			})
		},
		mixins: [presenter(), header(), form(defaultForm), crud()],
		data() {
			return {
				delLoading: false,
				permission: {
					add: ['admin', 'ftOrder:add'],
					edit: ['admin', 'ftOrder:edit'],
					del: ['admin', 'ftOrder:del']
				},
				scheduleList: [],
				schedule: '',
				orderId: '',
				scheduleName: '',
				updateScheduleDialog: false,
				remark: '',
				remarkDialog: false
			}
		},
		created() {
			this.crud.optShow = {
				add: false,
				edit: false,
				reset: true,
				download: true
			}
			this.dictDetail()
		},
		methods: {
			// 修改订单进度
			updateSchedule() {
				const that = this
				const params = {
					orderId: this.orderId,
					schedule: this.schedule
				}
				crudOrder.updateOrderSchedule(params).then(res => {
					this.crud.notify('修改成功', 'success')
					this.schedule = ''
					const list = this.crud.data
					for (let i = 0; i < list.length; i++) {
						if (that.orderId === list[i].id) {
							list[i].schedule = that.scheduleName
						}
					}
				}).catch(res => {
					this.crud.notify('修改失败', 'error')
				})
				this.updateScheduleDialog = false
			},
			// 获取进度
			dictDetail() {
				const params = {
					dictName: 'schedule'
				}
				setTimeout(() => {
					crudOrder.dictDetail(params).then(res => {
						this.scheduleList = res.schedule
					})
				}, 100)
			},
			// 打开进度修改谈话框
			openUpdateScheduleDialog(orderId, schedule) {
				const that = this
				this.orderId = orderId
				for (let i = 0; i < this.scheduleList.length; i++) {
					if (schedule === this.scheduleList[i].value) {
						that.schedule = that.scheduleList[i].label
					}
				}
				this.updateScheduleDialog = true
			},
			// 选择进度
			scheduleChange(e) {
				const that = this
				for (let i = 0; i < that.scheduleList.length; i++) {
					if (e === that.scheduleList[i].label) {
						that.scheduleName = that.scheduleList[i].value
					}
				}
			},
			// 打开修改备注谈话框
			openRemarkDialog(orderId, remark) {
				this.orderId = orderId
				this.remark = remark
				this.remarkDialog = true
			},
			// 修改备注
			updateRemark() {
				const that = this
				const params = {
					id: this.orderId,
					remark: this.remark
				}
				crudOrder.edit(params).then(res => {
					this.crud.notify('操作成功', 'success')
					const list = this.crud.data
					for (let i = 0; i < list.length; i++) {
						if (that.orderId === list[i].id) {
							list[i].remark = that.remark
						}
					}
					this.remark = ''
				}).catch(res => {
					this.crud.notify('操作失败', 'error')
				})
				this.remarkDialog = false
			}
		}
	}
</script>
